package jsu.xxy.coffee3.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jsu.xxy.coffee3.domain.Comments;
import jsu.xxy.coffee3.domain.com_score;
import jsu.xxy.coffee3.service.CommentsService;
import jsu.xxy.coffee3.mapper.CommentsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
* @author 张从轩
* @description 针对表【comments】的数据库操作Service实现
* @createDate 2024-01-02 17:05:12
*/
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments>
    implements CommentsService{
    @Autowired
    CommentsMapper commentsMapper;
    @Override
    public boolean addComment(int orderid, String comment, double score, String userName) {
        if(commentsMapper.selectById(orderid) != null) return false;
        LocalDateTime time = LocalDateTime.now();
        Comments comments = new Comments(orderid,comment,score,time,userName);
        return commentsMapper.insert(comments) > 0;
    }

    @Override
    public com_score getAllComments() {
        List<Comments> comments =  commentsMapper.selectList(null);
        double score = 0;
        for(int i = 0 ; i < comments.size() ; i ++){
            score += comments.get(i).getScore();
        }

        com_score score1 = new com_score(String.format("%.1f", score/comments.size()), comments);
        return score1;
    }

    @Override
    public Page<Comments> getCommentsByGBK(String com, String key, Integer pageNo, Integer pageSize) {
        Page<Comments> page = new Page<>();
        System.out.println(com+" " + key+" "+pageNo+" " +pageSize);
        QueryWrapper<Comments> queryWrapper = new QueryWrapper<>();
        if(com.equals("1") ){
            if(key != null){
                queryWrapper.gt("score",2).like("comment",key);
            }else queryWrapper.gt("score",2);

        }else if(com.equals("2") ){
            if(key != null){
                queryWrapper.lt("score",3).like("comment",key);
            }else queryWrapper.lt("score",3);
        }
        else {
            if(key != null){
                queryWrapper.like("comment",key);
            }
        }
        commentsMapper.selectPage(page,queryWrapper);
        return page;
    }

    @Override
    public Page<Comments> getAllCommentsPage(Integer pageNo, Integer pageSize) {
        Page<Comments> page = new Page<>(pageNo,pageSize);
        return commentsMapper.selectPage(page,null);
    }

}




